package com.deyuanyun.pic.insp.service.inspresult.impl;

import com.deyuanyun.pic.insp.mapper.InspCommMapper;
import com.deyuanyun.pic.insp.service.inspresult.InspCommService;
import com.deyuanyun.pic.insp.util.InspTaskVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/10/21）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
@Service
public class InspCommServiceImpl implements InspCommService {

    @Resource
    private InspCommMapper inspCommMapper;



    public List<HashMap<String, Object>> findTask(String lineId, String sendPig, String backPig) {
        return inspCommMapper.findTask(lineId, sendPig, backPig);
    }

    public List<HashMap<String, Object>> taskByLineId(String lineId) {
        return inspCommMapper.taskByLineId(lineId);
    }
    public List<HashMap<String, Object>> taskByLineIdOrMileage(String lineId, BigDecimal beginMileage, BigDecimal endMileage)
    {
        return inspCommMapper.taskByLineIdOrMileage(lineId,beginMileage,endMileage);
    }

    @Override
    public Map<String, Object> queryLineMileage(Map<String, Object> param) {
        return inspCommMapper.queryLineMileage(param);
    }

    @Override
    public List<Map<String, Object>> getNewTask(Map<String, Object> param) {
        return inspCommMapper.getNewTask(param);
    }

    @Override
    public Map<String, Object> queryTaskMileage(String taskId) {
        return inspCommMapper.queryTaskMileage(taskId);
    }


    /**
     * @param lineId
     * @param beginMileage
     * @param endMileage
     * @return
     */
    public List<InspTaskVO> queryNewTaskMileage(String lineId,Double beginMileage,Double endMileage)
    {

        List<String> taskIdList = inspCommMapper.queryTaskAllByLineId(lineId);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("lineId",lineId);

        if(beginMileage==null || endMileage == null)
        {
            Map<String,Object> map =  inspCommMapper.queryLineMileage(param);

            if(beginMileage == null && map.get("BEGINMILEAGE") != null)
            {
                beginMileage = ((BigDecimal)map.get("BEGINMILEAGE")).doubleValue();
            }

            if(endMileage == null  && map.get("ENDMILEAGE") != null)
            {
                endMileage = ((BigDecimal)map.get("ENDMILEAGE")).doubleValue();
            }
        }
        param.put("beginMileage",beginMileage);
        param.put("endMileage",endMileage);
        param.put("taskIdList",taskIdList);
        if(taskIdList.size() == 0)
        {
            taskIdList.add("-");
        }
        List<InspTaskVO> data = inspCommMapper.queryNewTaskMileage(param);
        List<InspTaskVO> result = handleMileage(data,beginMileage,endMileage);
        if(result.size() == 0)
        {
            InspTaskVO vo = new InspTaskVO();
            vo.setId("1");
            vo.setFromMileage(0d);
            vo.setToMileage(0d);
            result.add(vo);
        }
        return result;


    }


    private List<InspTaskVO> handleMileage(List<InspTaskVO> data,Double beginMileage,Double endMileage)
    {
        List<MileageBean> wait = new ArrayList<MileageBean>();
        wait.add(new MileageBean(beginMileage,endMileage));
        List<InspTaskVO> result = new ArrayList<InspTaskVO>();

        for (InspTaskVO itv:data)
        {
            for (int index=0,e=wait.size();index <wait.size();)
            {
                MileageBean mb = wait.get(index);
                switch (parse(itv,mb.b,mb.e))
                {
                    case 0:{
                        //检测任务的起点和终点完整的包含比较的这个绝对距离
                        InspTaskVO tx = new InspTaskVO();
                        tx.setId(itv.getId());

                        tx.setFromMileage(mb.b);
                        tx.setToMileage(mb.e);
                        tx.setPagDateTime(itv.getPagDateTime());

                        result.add(tx);

                        //移除待比较对象
                        wait.remove(index);

                        break;
                    }
                    case 1:{
                        //检测任务的起点和终点位于比较对象起点的左右

                        InspTaskVO tx = new InspTaskVO();
                        tx.setId(itv.getId());

                        tx.setFromMileage(mb.b);
                        tx.setToMileage(itv.getToMileage());
                        tx.setPagDateTime(itv.getPagDateTime());

                        result.add(tx);
                        //重置待比较对象的起点,将待比较对象的起点向右移动到检测任务的终点
                        mb.b =itv.getToMileage();
                        index++;
                        break;
                    }
                    case 2:{

                        //检测任务的起点和终点在 待比较距离之间或者等于待比较的距离
                        if(itv.getFromMileage() > mb.b)
                        {//在待比较起点的右边

                            MileageBean t = new MileageBean(mb.b,itv.getFromMileage());
                            wait.add(t);

                        }

                        if(itv.getToMileage() < mb.e)
                        {//在待比较终点的左边

                            MileageBean t = new MileageBean(itv.getToMileage(),mb.e);
                            wait.add(t);
                        }

                        InspTaskVO tx = new InspTaskVO();
                        tx.setId(itv.getId());

                        tx.setFromMileage(itv.getFromMileage());
                        tx.setToMileage(itv.getToMileage());
                        tx.setPagDateTime(itv.getPagDateTime());

                        result.add(tx);

                        //删除原有待比较对象
                        wait.remove(index);
                        break;
                    }

                    case 3:{
                        //检测任务的起点和终点位于比较对象终点的左右
                        //检测任务的起点和终点位于比较对象起点的左右


                        InspTaskVO tx = new InspTaskVO();
                        tx.setId(itv.getId());

                        tx.setFromMileage(itv.getFromMileage());
                        tx.setToMileage(mb.e);
                        tx.setPagDateTime(itv.getPagDateTime());

                        result.add(tx);


                        //重置待比较对象的起点,将待比较对象的起点向右移动到检测任务的终点
                        mb.e =itv.getFromMileage();
                        index++;
                        break;
                    }

                    default:{
                        index ++;
                    }
                }
            }
        }

        return result;
    }


    private int parse(InspTaskVO itv,Double beginMileage,Double endMileage)
    {


        if(beginMileage == endMileage)
        {

            return -1;

        }else if(itv.getFromMileage() <= beginMileage && itv.getToMileage() >= endMileage)
        {

            return 0;

        }else if(itv.getFromMileage() <= beginMileage && itv.getToMileage() <= endMileage && itv.getToMileage() >= beginMileage) {

           return  1;
        }else if(itv.getFromMileage() >= beginMileage && itv.getToMileage() <= endMileage )
        {

           return 2;
        }else if(itv.getFromMileage() >= beginMileage && itv.getToMileage() >= endMileage && itv.getFromMileage() <= endMileage)
        {

            return 3;
        }
        return -1;
    }


    class MileageBean
    {
        Double b;
        Double e;
        public MileageBean(Double b,Double e)
        {
            this.b = b;
            this.e = e;
        }

        public Double getB() {
            return b;
        }

        public void setB(Double b) {
            this.b = b;
        }

        public Double getE() {
            return e;
        }

        public void setE(Double e) {
            this.e = e;
        }
    }
}
